const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha";

async function main() {
  const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
  const db = client.db('taosha');


  let topics = await db.collection('topics')
    .find(
      {title: /​/}
    )
    .toArray();


  for (let i = 0; i < topics.length; i++) {
    try {
      let topic = topics[i];
      let title = topic.title;
      console.log(title);
      title = title.replace(/\u200B/g, '').trim();
      console.log(title);
      let result = await db.collection('topics').updateOne({_id: topic._id}, {
        $set: {
          title: title
        },
      });
      console.log(result.result, i, topics.length);
    } catch (e) {
      console.warn(e.message, i, topics.length);
    }
  }

  client.close();

}

main();
